<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->headerdata = array();
		if (($this->session->userdata('adminid')>0) && ($this->session->userdata('adminhash')==md5($this->session->userdata('adminid').$this->session->userdata('adminemail')))){
			$this->headerdata['adminid'] = $this->session->userdata('adminid');
			$this->headerdata['adminemail'] = $this->session->userdata('adminemail');
			$this->headerdata['username'] = $this->session->userdata('adminemail');
		}else{
			$this->headerdata['adminid'] = 0;
			$this->headerdata['adminemail'] = 'Guest';
			$this->headerdata['username'] = 'Guest';
		}
		
		if (($this->session->userdata('msg') != '') && ($this->session->userdata('msgtype') != '')){
			$this->headerdata['msg'] = $this->session->userdata('msg');
			$this->headerdata['msgtype'] = $this->session->userdata('msgtype');
			$this->session->set_userdata('msg','');
			$this->session->set_userdata('msgtype','');
		}
		
		$this->headerdata['route'] = 'AdminCP';
		$this->headerdata['routelink'] = 'admin';
	}
	
	function index(){
		if ($this->headerdata['adminid'] == 0) redirect(base_url().'/index.php/admin/login');
		$this->headerdata['route2'] = 'Home';
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		//$this->load->view('admin/index');
		$this->load->view('admin/common/footer');
	}
	
	function login(){
		if ($this->headerdata['adminid'] > 0) redirect(base_url('index.php/admin'));
		$email = '';
		$error = '';
		
		if ($this->input->post('email')!= '' || $this->input->post('password')!= ''){
			$email = $this->input->post('email');
			$password = $this->input->post('password');
			if ($email == ''){
				$error = 'Username cannot be empty.';
			}else if ($password == ''){
				$error = 'Password cannot be empty.';
			}else{
				// Login admin
				$this->load->model('AdminModel');
				$admin = $this->AdminModel->login($email,$password);
				if (isset($admin->email)){
					$this->session->set_userdata('adminid',$admin->id);
					$this->session->set_userdata('adminemail',$admin->email);
					$this->session->set_userdata('adminhash',md5($admin->id.$admin->email));
					redirect(base_url('/index.php/admin/home/index'));
				}else{
					$error = 'Username and password do not match.';
				}
			}
		}
		
		$data = array();
		$data['error'] = $error;
		$data['email'] = $email; 
		
		$this->load->view('admin/login',$data);
	}
	
	function logout(){
		$this->session->sess_destroy();
		redirect(base_url('/index.php/admin/login'));
	}
	
	function option(){
		if ($this->headerdata['adminid'] == 0) redirect(base_url().'/index.php/admin');
		$this->headerdata['route2'] = 'Option';
		
		if (count($this->input->post())){
			if ($this->input->post('fromdate') != ''){
				$tmp = strtotime($this->input->post('fromdate'));
				//$this->db->where('name','fromdate');
				$this->db->update('option',array('startdate'=>$tmp));
			}
			if ($this->input->post('todate') != ''){
				$tmp = strtotime($this->input->post('todate'));
				//$this->db->where('name','todate');
				$this->db->update('option',array('enddate'=>$tmp));
			}
			// if ($this->input->post('restrictMax') != ''){
			// 	$tmp = $this->input->post('restrictMax');
			// 	$this->db->where('name','restrictMax');
			// 	$this->db->update('option',array('value'=>$tmp));
			// }
		}
		
		$data = array();
		$q = $this->db->get('option');
		// $option = array();
		// foreach ($q->result() as $item){
		// 	$option[$item->name] = $item->value;
		// }
		$data['option'] = $q->result();
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/option',$data);
		$this->load->view('admin/common/footer');
	}
}
?>